<template>
  <el-container>
    <el-aside width="201px">
      <NavMenu/>
    </el-aside>
    <el-container>
      <el-header>
        <strong>Server-System后台管理系统V1.0</strong>
        <div class="header-avatar">
          <el-avatar class="el-avatar" size="medium" :src="userInfo.avatar"></el-avatar>
          <el-dropdown>
            <span class="el-dropdown-link">
              {{userInfo.username}}<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <router-link :to="{name:'UserCenter'}">个人中心</router-link>
              </el-dropdown-item>
              <el-dropdown-item @click.native="logout">退出系统</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <!-- 配置菜单中子菜单链接，子路由的出口 -->
        <Tabs/>
        <div style="margin:0 15px">
          <router-view></router-view>
        </div>
        
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import NavMenu from "@/components/NavMenu.vue";
import Tabs from '@/components/Tabs.vue';

export default {
  data() {
    return {
      //登录之后，存储登录用户基本信息：头像、用户名
      userInfo:{
        id:'',
        username:'',
        avatar: '' 
      }
    }
  },
  created() {
    //生命周期钩子函数：
    this.getUserInfo();
  },
  methods: {
    //获得登录用户基本数据
    getUserInfo(){
      //请求Java服务器端，获得用户基本数据。
      this.$axios.get('/api/userinfo').then(response=>{
        this.userInfo = response.data.resultdata
      })
    },
    //退出系统的方法
    logout(){
      //SpringSecurity会拦截/login 和 /logout
      this.$axios.get('/logout').then(response=>{
        //清除vuex中存储token
        this.$store.commit('SET_TOKEN','');
        //清除localStorage
        sessionStorage.clear();
        localStorage.clear();
        //退出成功，重新跳转回登录页面
        this.$router.push('/login')
      }).catch(e=>{
        console.log(e);
      })
    }
  },
  components:{
    NavMenu,
    Tabs,
  }
}
</script>

<style scoped>
  strong{ color:#409eff}
  .el-container{ padding:0; margin:0;height:100%; }
  .el-aside{  background-color:#efefef; }
  .el-header{ 
    /* background-image:radial-gradient(circle farthest-side at 20px -122px,#f793a8,#fdcdbf,#78dcde); */
    color:#333;text-align:left; line-height:60px; }
  .el-main{ color:#000; padding:0; }
  .header-avatar{
    float:right;
    display:flex;
    justify-content:spae-around;
    align-items:center;
  }
  .el-avatar{ margin-right:5px; }
  a{ color:#000; text-decoration:none; }
  .el-dropdown-link{ cursor:pointer }
</style>
